package com.lxxin.controller;

import com.github.pagehelper.PageInfo;
import com.lxxin.pojo.*;
import com.lxxin.service.BlogService;
import com.lxxin.service.TagService;
import com.lxxin.service.TypeService;
import com.lxxin.utils.RedisUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/home")
public class HomeController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;


    @GetMapping("/findBlogList")
    @ApiOperation("首页查询博客的分页列表")
    public Result findBlogList(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        PageInfo<Blog> homeBlogList = blogService.findHomeBlogList(currentPage, pageSize);
        List<Long> ids = new ArrayList<>();
        homeBlogList.getList().forEach(blog -> {
            ids.add(blog.getId());
        });
        List<Long> viewsList = blogService.findViewsByIds(ids);
        homeBlogList.getList().forEach(blog -> {
            blog.setViews(viewsList.get(0));
            viewsList.remove(0);
        });
        return Result.builder()
                .statusCode(HttpStatus.OK.value())
                .flag(true)
                .data(homeBlogList)
                .build();
    }

    @GetMapping("/findTypeList")
    @ApiOperation("查询首页分类列表博客最多前五条")
    public Result findTypeList(){
        List<Type> topTypeList = typeService.findTopTypeList(5);
        return Result.builder()
                .statusCode(HttpStatus.OK.value())
                .flag(true)
                .data(topTypeList)
                .build();
    }

    @GetMapping("/findTagList")
    @ApiOperation("查询首页标签列表博客最多前十二条")
    public Result findTagList() {
        List<Tag> topTagList = tagService.findTopTagList(12);
        return Result.builder()
                .statusCode(HttpStatus.OK.value())
                .flag(true)
                .data(topTagList)
                .build();
    }

    @GetMapping("/findRecommendList")
    @ApiOperation("查询首页最新推荐博客列表前五条")
    public Result findRecommendList() {
        List<Blog> topRecommendBlogList = blogService.findTopRecommendBlogList(5);
        return Result.builder()
                .statusCode(HttpStatus.OK.value())
                .flag(true)
                .data(topRecommendBlogList)
                .build();
    }

    @GetMapping("/findSearchBlogList")
    @ApiOperation("查询符合搜索条件的博客列表")
    public Result findSearchBlogList(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "search", defaultValue = "") String search) {
        PageInfo<Blog> searchBlog = blogService.findSearchBlog(currentPage, pageSize, search);
        //当查询结果不为空时, 执行操作
        if(searchBlog.getTotal() != 0) {
            List<Long> ids = new ArrayList<>();
            searchBlog.getList().forEach(blog -> {
                ids.add(blog.getId());
            });
            List<Long> viewsList = blogService.findViewsByIds(ids);
            searchBlog.getList().forEach(blog -> {
                blog.setViews(viewsList.get(0));
                viewsList.remove(0);
            });
        }
        return Result.builder()
                .statusCode(HttpStatus.OK.value())
                .flag(true)
                .data(searchBlog)
                .build();
    }

    @GetMapping("/findCarouselList")
    @ApiOperation("查询轮播图列表")
    public Result findCarouselList() {
        List<Carousel> carouselList = blogService.findCarouselList();
        return Result.builder()
                .flag(true)
                .statusCode(HttpStatus.OK.value())
                .data(carouselList)
                .build();
    }

    @GetMapping("/findCountList")
    @ApiOperation("查询博客、分类、标签总数")
    public Result findCountList() {
        Count count = blogService.findCount();
        List<Count> countList = new ArrayList<>();
        countList.add(count);
        return Result.builder()
                .statusCode(HttpStatus.OK.value())
                .flag(true)
                .data(countList)
                .build();
    }

    @GetMapping("/findAllWordCount")
    @ApiOperation("查询所有文章总字数")
    public Result findAllWordCount(){
        Long count = blogService.findAllWordCount();
        return Result.builder()
                .statusCode(HttpStatus.OK.value())
                .flag(true)
                .data(count)
                .build();
    }
}
